package homework.day03;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

/**
 * 将Test01当前目录下的所有注册的User对象对应的obj文件获取到，并进行
 * 反序列化后输出每个用户的信息(直接输出反序列化后的User对象即可)
 * @author Xiloer
 *
 */
public class Test05 {
    public static void main(String[] args) {
        //1获取当前目录下的所有.obj文件
        File dir = new File(".");
        File[] subs = dir.listFiles(f->f.getName().endsWith(".obj"));
        //2遍历每一个obj文件并反序列化
        for(File sub : subs){
            try (
                    FileInputStream fis = new FileInputStream(sub);
                    ObjectInputStream ois = new ObjectInputStream(fis);
            ){
                Object obj = ois.readObject();
                if(obj instanceof User) {
                    User user = (User)obj;
                    System.out.println(user);
                }
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
}
